﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Benefit.Models.View
{
    /// <summary>
    /// 操作员月Tick操作情况
    /// </summary>
    public class OperatorMonthTick
    {
        public int Id { get; set; }
        /// <summary>
        /// 账户
        /// </summary>
        public int OperatorId { get; set; }

        public virtual Sys.Operator Operator { get; set; }

        public int Month { get; set; }

        public int Year { get; set; }

        /// <summary>
        /// 是否亏损
        /// </summary>
        public bool Win { get; set; }
        /// <summary>
        /// 发生TICK值
        /// </summary>
        public int Tick { get; set; }
        /// <summary>
        /// 次数
        /// </summary>
        public int Count { get; set; }
        /// <summary>
        /// 合约编号
        /// </summary>
        public int InstrumentId { get; set; }
        /// <summary>
        /// 多空方向
        /// </summary>
        public int Direction { get; set; }
        ///// <summary>
        ///// 得到操作员月盈利或亏损的Tick数量
        ///// </summary>
        ///// <param name="operatorid"></param>
        ///// <param name="tradeHistoryId"></param>
        ///// <param name="win"></param>
        ///// <returns></returns>
        //public List<OperatorMonthTick> GetOperatorMonthWinOrLostTicks(int operatorid, int month, int year, bool win, DB.DBManager db)
        //{
        //    var query = db.OperatorMonthTick.Where(a => a.OperatorId == operatorid).Where(a => a.Month == month).Where(a => a.Year == year).Where(a => a.Win == win).OrderBy(a => a.Tick);
        //    List<OperatorMonthTick> returnValues = new List<OperatorMonthTick>();

        //    for (int i = 1; i < 11; i++)
        //    {
        //        OperatorMonthTick tick = new OperatorMonthTick();
        //        tick.Tick = i;
        //        var query1 = query.Where(a => a.Tick == tick.Tick);
        //        if (query1 == null || query1.Count() == 0)
        //        {
        //            tick.Count = 0;
        //        }
        //        else
        //        {
        //            tick.Count = query1.Sum(a => a.Count);
        //        }

        //        returnValues.Add(tick);
        //        tick = null;
        //    }
        //    OperatorMonthTick tick2 = new OperatorMonthTick();
        //    tick2.Tick = 11;
        //    var query2 = query.Where(a => a.Tick > 10);
        //    if (query2.Count() > 0)
        //    {
        //        tick2.Count = query2.Sum(a => a.Count);
        //    }
        //    else
        //    {
        //        tick2.Count = 0;
        //    }

        //    returnValues.Add(tick2);
        //    return returnValues;
        //}

        //public void InitOperatorMonthTick(int month, int year, DB.DBManager db, bool win)
        //{
        //    List<OperatorMonthTick> ticks = new List<OperatorMonthTick>();
        //    List<Sys.Operator> operators = new Sys.Operator().GetUnDeletedOperators(db);
        //    foreach (Sys.Operator op in operators)
        //    {
        //        var query = db.OperatorDayTick.Where(a => a.OperatorId == op.Id).Where(a => a.TradeHistory.YPdate == year).Where(a => a.TradeHistory.MPdate == month).ToList();

        //        if (query.Count() > 0)
        //        {
        //            foreach (BenefitHelper.View.OperatorDayTick t in query)
        //            {
        //                var queryExist = ticks.Where(a => a.OperatorId == op.Id).Where(a => a.Direction == t.Direction).Where(a => a.InstrumentId.Equals(t.InstrumentId)).Where(a => a.Win == t.Win).Where(a => a.Tick == t.Tick).ToList();
        //                if (queryExist.Count() == 0)
        //                {
        //                    var querySum = query.Where(a => a.Direction == t.Direction).Where(a => a.InstrumentId.Equals(t.InstrumentId)).Where(a => a.Win == t.Win).Where(a => a.Tick == t.Tick).ToList();
        //                    BenefitHelper.View.OperatorMonthTick tick = new OperatorMonthTick();
        //                    tick.Count = querySum.Sum(a => a.Count);
        //                    tick.Direction = t.Direction;
        //                    tick.InstrumentId = t.InstrumentId;
        //                    tick.Month = month;
        //                    tick.OperatorId = op.Id;
        //                    tick.Tick = t.Tick;
        //                    tick.Win = t.Win;
        //                    tick.Year = year;
        //                    ticks.Add(tick);
        //                }
        //            }
        //        }
        //    }
        //    foreach (View.OperatorMonthTick tick in ticks)
        //    {
        //        var queryExist = db.OperatorMonthTick.Where(a => a.OperatorId == tick.OperatorId).Where(a => a.Direction == tick.Direction).Where(a => a.InstrumentId.Equals(tick.InstrumentId)).Where(a => a.Tick == tick.Tick).Where(a => a.Win == tick.Win).Where(a => a.Month == tick.Month).Where(a => a.Year == tick.Year);
        //        if (queryExist.Count() > 0)
        //        {
        //            queryExist.First().Count = tick.Count;
        //        }
        //        else
        //        {
        //            db.OperatorMonthTick.Add(tick);
        //        }
        //    }

        //    db.SaveChanges();
        //}

        //public void Delete(int month, int year, DB.DBManager db)
        //{
        //    var query = db.OperatorMonthTick.Where(a => a.Year == year).Where(a => a.Month == month);
        //    foreach (OperatorMonthTick t in query)
        //    {
        //        db.OperatorMonthTick.Remove(t);
        //    }
        //    db.SaveChanges();
        //}
    }
}
